package com.example.demo.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Alipay {
     @Value("${alipay.appid}")
     private String appid;
     @Value("${alipay.url}")
     private String url;
     @Value("${alipay.privateKey}")
     private String privateKey;
     @Value("${alipay.publicKey}")
     private String publicKey;
     @Value("${alipay.notifyUrl}")
     private String notifyUrl;
     @Value("${alipay.retureUrl}")
     private String retureUrl;



     public String pay(String name, String email ,String money, String phone) {
         AlipayClient alipayClient = new DefaultAlipayClient(url,appid,privateKey,"json","UTF-8",publicKey,"RSA2");
         AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
         request.setNotifyUrl(notifyUrl);
         request.setReturnUrl(retureUrl);
         JSONObject bizContent = new JSONObject();
         bizContent.put("out_trade_no", email);
         bizContent.put("total_amount", money);
         bizContent.put("subject", phone);
         bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
         request.setBizContent(bizContent.toString());
         AlipayTradePagePayResponse response = null;
         String form = null;
         try {
             response = alipayClient.pageExecute(request);
             form=response.getBody();
         } catch (AlipayApiException e) {
             e.printStackTrace();
         }
         if(response.isSuccess()){
             System.out.println("调用成功");
         } else {
             System.out.println("调用失败");
         }
         return form;
     }


     public  String query(String id){
         AlipayClient alipayClient = new DefaultAlipayClient(url,appid,privateKey,"json","UTF-8",publicKey,"RSA2");
         AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
         JSONObject bizContent = new JSONObject();
         bizContent.put("out_trade_no", id);
         request.setBizContent(bizContent.toString());
         AlipayTradeQueryResponse response = null;
         String body=null;
         try {
             response = alipayClient.execute(request);
              body = response.getBody();
         } catch (AlipayApiException e) {
             e.printStackTrace();
         }
         if(response.isSuccess()){
             System.out.println("调用成功");
         } else {
             System.out.println("调用失败");
         }
         return body;
     }


}
